/**
 * @author baifengming
 * 2021-06-20
 */
/* eslint-disable */

var _sursen_ofd_reader_ofd_ = {};
var SursenSeal = {};
var _g_sursen_ofd_reader_ofd_ = {};
/**
 * 12 对象加载接口
 * @param {Object} parentNode
 * @param {Object} objid
 */
_sursen_ofd_reader_ofd_.loadReader = function (parentId) {
  var sursenobj = _sursen_ofd_reader_ofd_.S_loadReaderEx(parentId, true);
  return sursenobj;
}


SursenSeal.loadSursenSeal = function (divid) {

  try {
    
    _sursen_ofd_reader_ofd_.S_loadReaderEx(divid, false);
    

  } catch (e) {
    console.log(e);
  }


}

/**
 * 13 用印文件预览接口
 * @param {Object} _sursen_ofd_reader_ofd_  ofd阅读器对象
 * @param {Object} fileurl 远程URL
 * @param {Object} isreadonly 是否只读
 */
_sursen_ofd_reader_ofd_.openOfdFile = function (sursenobj, surl, readonly) {
 
  var url=_sursen_ofd_reader_ofd_.getFilebyHttps(sursenobj, surl);
  url =url + "&randomid=" + _sursen_ofd_reader_ofd_.getGUID(); 
  var rt = sursenobj.sursenReader.openFile(url, readonly);
  if (!rt) {
    throw Error('打开文档失败!');
  }
 
  sursenobj.currentofdfile = url;
  sursenobj.openStatus = readonly;
  var CompName = 'f_save,f_open,w_navigator,f_print';

  var car = CompName.split(',');
  for (var i = 0; i < car.length; i++) {
    sursenobj.sursenReader.setCompositeVisible(car[i], false);
  }
  sursenobj.sursenReader.setCompositeVisible('f_saveas', true);
  _sursen_ofd_reader_ofd_.setZoomRadio(sursenobj,75);
  return rt;
}
/**
 * 14.文件保存接口
 * @param {Object} sursenobj
 * @param {Object} url
 */
_sursen_ofd_reader_ofd_.saveOfdFile = function (sursenobj, url) {
  var sok = false;
  var rurl=url;
  var surl=url.toLowerCase();
  if(surl.indexOf("https")!=-1){  
		 rurl=_sursen_ofd_reader_ofd_.getFilebyHttps(sursenobj, url);
    }
	     var reg = /&/g;
		  var strURL = rurl.replace(reg, '@@');
		  strURL = encodeURI(encodeURI(strURL));
		  var proxy = sursenobj.Proxy;		  
		  sok = false;
		  sok = sursenobj.sursenReader.saveFile(proxy + strURL);

  return sok;

}

_sursen_ofd_reader_ofd_.getFilebyHttps = function (sursenobj, url) {
  var rUrl=url;
  var netUrl = sursenobj.ProxyNetGate;  
 
  var surl=url.toLowerCase();
  if(surl.indexOf("https")!=-1){
      var endstr=url.substring(10,url.length);
      var frontstr=url.substring(0,10);
      var str=endstr.substring(0,endstr.indexOf("/"));
	  str =frontstr + str;
	  rUrl=rUrl.replace(str,netUrl);	  
	  
  }
  
  return rUrl;
 
}

//基于http协议文件保存
_sursen_ofd_reader_ofd_.saveOfdFileByHttp = function (sursenobj, url) {
  var reg = /&/g;
  var strURL = url.replace(reg, '@@');
  strURL = encodeURI(encodeURI(strURL));
  var proxy = sursenobj.Proxy;
  console.log('保存OFD文档:' + (proxy + strURL));
  var sok = false;
  sok = sursenobj.sursenReader.saveFile(proxy + strURL);
  return sok;
}

/**
 * 14.文件保存接口
 * @param {Object} sursenobj
 * @param {Object} url
 * 返回json串
 */
_sursen_ofd_reader_ofd_.saveOfdFileEx = function (sursenobj, url) {
  return sursenobj.sursenReader.saveFileUrl(url);
}
/**
 * 15 用印文件打印接口
 * @param {Object} sursenobj
 * @param {Object} count
 */
/**
_sursen_ofd_reader_ofd_.printOfdFile=function(sursenobj,count){
	var smsg="";
	var srcFilrUrl=sursenobj.currentofdfile;
	var sn=_sursen_ofd_reader_ofd_.PrintDocEx(sursenobj,srcFilrUrl,count);
	if(sn==0){
	  smsg=""; 	
	}else{
	  smsg="打印失败，错误编号为：" + sn;  
        }
	if(sn==255){
	  smsg="不支持您选择的虚拟打印机"; 	
	}
	if(smsg.length>1) throw Error(smsg);
	
}**/
/**
 * 19 指定印章接口
 * @param {Object} sursenobj
 * @param {Object} sealId
 */
_sursen_ofd_reader_ofd_.setSealId = function (sursenobj, sealId) {
  sursenobj.sursenReader.setSealId(sealId);
}

/**
 * 20 打印设置
 * @param {Object} readerParent
 * @param {Object} nCounts
 */
_sursen_ofd_reader_ofd_.showPrintSetting = function () {
  var dobj = _sursen_ofd_reader_ofd_.getSedOI();
  var hClose = dobj.S_SetPrinterParamsUi(0);
  return hClose;
}

/**
 * 21 获取印章名称，名称之间用分号分隔
 * @param {Object} readerParent
 * @param {Object} nCounts
 */
_sursen_ofd_reader_ofd_.getSealList = function (sursenobj) {
  var sealname = sursenobj.sursenReader.getSealList(0);
  return sealname;
}

/**
 * 22 文件关闭接口，需要在页面关闭时，调用此接口，以释放资源。
 * @param {Object} sursenobj
 */
_sursen_ofd_reader_ofd_.closeOfdFile = function (sursenobj) {
  return sursenobj.sursenReader.closeFile();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//7.3.4 关闭文档
_sursen_ofd_reader_ofd_.closeFile = function (sursenobj) {
  return sursenobj.sursenReader.closeFile();
}
//7.4.1 显示/隐藏组件
_sursen_ofd_reader_ofd_.setCompositeVisible = function (sursenobj, CompName, bVisible) {
  sursenobj.sursenReader.setCompositeVisible(CompName, bVisible);
}
//7.4.2 启用/禁用组件
_sursen_ofd_reader_ofd_.setCompositeEnable = function (sursenobj, strCompName, bEnable) {
  sursenobj.sursenReader.setCompositeEnable(strCompName, bEnable);
}
//7.4.3 设置视图首选项
_sursen_ofd_reader_ofd_.setViewPreference = function (sursenobj, key, val) {
  sursenobj.sursenReader.setViewPreference(key, val);
}
//7.4.4 设置页面显示比例
_sursen_ofd_reader_ofd_.setZoomRadio = function (sursenobj, zoomValue) {
  sursenobj.sursenReader.setZoomRadio(zoomValue);
}
//7.4.5 获取页面显示比例
_sursen_ofd_reader_ofd_.getZoomRadio = function (sursenobj) {
  return sursenobj.sursenReader.getZoomRadio();
}
//7.5.1 设置用户名
_sursen_ofd_reader_ofd_.setUserName = function (sursenobj, userName) {
  return sursenobj.sursenReader.setUserName(userName)
}
//7.5.2 获取用户名
_sursen_ofd_reader_ofd_.getUserName = function (sursenobj) {
  return sursenobj.sursenReader.getUserName();
}
//7.5.3 设置印章标识
//7.5.4 设置文档元数据
_sursen_ofd_reader_ofd_.setMetaData = function (id, val) {
  _sursen_ofd_reader_ofd_.sursenReader.setMetaData(id, val);
}
//7.5.5 获取文档元数据
_sursen_ofd_reader_ofd_.getMetaData = function (id) {
  return _sursen_ofd_reader_ofd_.sursenReader.getMetaData(id);
}
//7.5.6 设置日志服务地址
_sursen_ofd_reader_ofd_.setLogSvrURL = function (url) {
  _sursen_ofd_reader_ofd_.sursenReader.setLogSvrURL(url);
}
//7.6.1 添加追踪水印
_sursen_ofd_reader_ofd_.addTrackInfo = function (xmlParam) {
  _sursen_ofd_reader_ofd_.sursenReader.addTrackInfo(xmlParam);
}
//7.6.2 清除追踪水印
_sursen_ofd_reader_ofd_.clearTrackInfo = function () {
  _sursen_ofd_reader_ofd_.sursenReader.clearTrackInfo();
}
//7.7 获取语义内容
_sursen_ofd_reader_ofd_.getTaggedText = function (tagId) {
  return _sursen_ofd_reader_ofd_.sursenReader.getTaggedText(tagId);
}
//7.8 获取日志文件路径
_sursen_ofd_reader_ofd_.getLogFilePath = function () {
  return _sursen_ofd_reader_ofd_.sursenReader.getLogFilePath();
}

//4.设置软件模式
_sursen_ofd_reader_ofd_.setReaderMode = function (iMode) {
  _sursen_ofd_reader_ofd_.sursenReader.setReaderMode(iMode);
}

//设置印章名称
_sursen_ofd_reader_ofd_.setSealName = function (sursenobj, sealName) {
  sursenobj.sursenReader.setSealName(sealName);
}
//签名算法
_sursen_ofd_reader_ofd_.setSealSignMethod = function (sursenobj, signMethod) {
  sursenobj.sursenReader.setSealSignMethod(signMethod);
}
//
_sursen_ofd_reader_ofd_.signOnLastPosition = function (sursenobj) {
  sursenobj.sursenReader.signOnLastPosition();
}
//获取语义树
_sursen_ofd_reader_ofd_.getSemantic = function (sursenobj) {
  return sursenobj.sursenReader.getSemantic();
}
//设置ofdread窗体大小
_sursen_ofd_reader_ofd_.setWindowWidthAndHeight = function (sursenobj, width, height) {
  return sursenobj.sursenReader.setWindowWidthAndHeight(width, height);
}
//获取公文唯一标识
_sursen_ofd_reader_ofd_.getDocID = function (sursenobj) {
  return sursenobj.sursenReader.getDocID();
}

////////////////////////////////////20191211新增，实现打印控制功能/////////////////////////////////////////////////////
_sursen_ofd_reader_ofd_.S_loadReaderEx = function (objid, isshowreader) {
  //7.1 插件初始化

  var stylewh = "WIDTH='100%' HEIGHT='100%' ";
  var sursenofd = objid + "_sedois";
  var sursenReaderid = objid + "_reader";
  var sursenhttp = objid + "_http";
  var readerParent = {};
  if (isshowreader == false) {
    stylewh = "WIDTH='0' HEIGHT='0' ";
  }
  var objvid = document.getElementById(objid);
  objvid.innerHTML = "<embed id='" + sursenReaderid + "' TYPE='trivial/very'  " + stylewh + " text='' print='0' EleSeal='0' WriteString=''></embed>";
 
  readerParent.sursenofd= _sursen_ofd_reader_ofd_.getSedOI();
	   readerParent.sursenhttp = _sursen_ofd_reader_ofd_.getSedFun();	 
  readerParent.sursenReader = document.getElementById(sursenReaderid);
  readerParent.g_delfiels = "";
  readerParent.currentofdfile = "";
  readerParent.openStatus = false;
  readerParent.g_tmppath=_sursen_ofd_reader_ofd_.getTmpPath(readerParent.sursenhttp);
  console.log(readerParent.sursenReader.getPluginVersion());
  readerParent.Proxy = readerParent.sursenReader.ReadConfig("config_project_firefox.ini", "", "Proxy");
  readerParent.KeySearch = readerParent.sursenReader.ReadConfig("config_project_firefox.ini", "", "KeySearch");
  readerParent.ChangeServer = readerParent.sursenReader.ReadConfig("config_project_firefox.ini", "", "ChangeServer");
  readerParent.ProxyNetGate = readerParent.sursenReader.ReadConfig("config_project_firefox.ini", "", "ProxyNetGate");
  _g_sursen_ofd_reader_ofd_ = readerParent;
  return readerParent;
}

/**
_sursen_ofd_reader_ofd_.S_loadReader = function(parentNode, objid,isshowreader) {
  //7.1 插件初始化
  
  var stylewh = "WIDTH='100%' HEIGHT='100%' ";
   var sursenofd=objid+"_sedois";
  var sursenReader=objid + "_reader";
  var sursenhttp=objid + "_http";
  var readerParent={};
  if(isshowreader==false){
     stylewh = "WIDTH='0' HEIGHT='0' ";	
  } 
  parentNode.innerHTML ="<embed id='" + sursenReader + "' TYPE='trivial/very'  WIDTH='100%' HEIGHT='100%' text='' print='1' EleSeal='1' WriteString=''></embed>" +
                         "<embed id='" + sursenofd +  "' type=\"application/SursenOFD-plugin\" style=\"display: block; width: 0px; height: 0px;\"></embed>" + 
                         "<embed id='" + sursenhttp + "' type=\"application/httpconnect-plugin\" style=\"display: block; width: 0px; height: 0px;\"></embed>";
	
	readerParent.sursenofd=document.getElementById(sursenofd);
	readerParent.sursenhttp = document.getElementById(sursenhttp);
	readerParent.sursenReader = document.getElementById(sursenReader);;       
  
  //readerParent.sursenofd=document.getElementById(sofdid);
  //readerParent.sursenReader = document.getElementById(sursenreadid);
  //readerParent.sursenhttp = document.getElementById(shttpid);
  return readerParent;
}
**/

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////开始上传下载控制////////////////////////////////////


_sursen_ofd_reader_ofd_.downloadFile = function (sursenhttp, path, hurl, mbfile) {
  var sn = sursenhttp.HttpDownLoadFile(path, hurl, mbfile);
  var sfile = "";
  if (sn == 0) sfile = path + mbfile;
  return sfile;
}

/**
 * 完成文件上传功能
 * @param {Object} sfile
 * @param {Object} hurl
 */
_sursen_ofd_reader_ofd_.httpUpload_sendfun = function (sursenhttp, sfile, URL) {
  var sok = sursenhttp.UpLoadFileEx2(sfile, URL);
  //UpLoadFileEx2 
  return sok;
}



/**
 * 完成文件目录选择功能
 * @param {Object} sfile
 * @param {Object} hurl
 */
_sursen_ofd_reader_ofd_.GetDir = function (sursenhttp) {
  var sok = sursenhttp.G_GetSaveFileName();
  //UpLoadFileEx2 
  return sok;
}

/**
* 完成文件上传功能
* @param {Object} sfile
* @param {Object} hurl
* 返回json字符串
*/
_sursen_ofd_reader_ofd_.httpUploadEx_sendfun = function (sursenhttp, sfile, URL) {
  var sok = sursenhttp.G_saveFileUrl(sfile, URL);
  //UpLoadFileEx2 
  return sok;
}


/**
 * 删除文件
 * @param {Object} srcfiles
 */
_sursen_ofd_reader_ofd_.delFile = function (sursenhttp, srcfiles) {
  var sar = srcfiles.split(";");
  for (var i = 0; i < sar.length; i++) {
    var sfile = sar[i];
    sfile = sfile.replace(/\\/ig, "/");
    var sn = sursenhttp.G_DeleteFile(sfile);

  }

}

_sursen_ofd_reader_ofd_.getTmpPath = function (sursenhttp) {
  var tmp = sursenhttp.G_GetTemporary() + "/";
  
  return tmp;
}

/**
 * 获取唯一值
 */

_sursen_ofd_reader_ofd_.getGUID = function () {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}




/////////////////////////////////提供打印控制接口//////////////////////////////
//long S_SetPrinterParamsUi(long nType)
//printnum,unitname,deptname,username,wsmc,wsid

/**
_sursen_ofd_reader_ofd_.PrintDocEx=function(readerParent,srcFilrUrl,printnum){
	var sfile="";	
	var nprint = parseInt(printnum);	
	//if(srcFilrUrl.indexOf("http")!=-1) sfile=_sursen_ofd_reader_ofd_.downloadFileEx(readerParent,srcFilrUrl,"ofd");
      //  else sfile=srcFilrUrl;
	  _sursen_ofd_reader_ofd_.SetConSoleForWin(readerParent);
	var hDoc =  readerParent.sursenofd.S_OpenFile(srcFilrUrl, "");
 	var sn =readerParent.sursenofd.S_PrintDoc("",nprint,true,"");
 	var hClose = readerParent.sursenofd.S_CloseFileByHandle(hDoc);	
	//_sursen_ofd_reader_ofd_.delFile(readerParent,sfile);
	return sn;
}
**/

_sursen_ofd_reader_ofd_.SetConSoleForWin = function (sursenofd) {
  try {
    var sn = sursenofd.G_SetConSoleForWin();
  } catch (e) {
    alert(e.message);
  }
}


_sursen_ofd_reader_ofd_.selectFile = function (sursenofd) {
  return sursenofd.G_GetOpenFileName();
  //return _g_sursen_ofd_reader_ofd_.sursenofd.G_GetSaveFileName();
}


_sursen_ofd_reader_ofd_.setPath = function (sursenofd) {
  //return _g_sursen_ofd_reader_ofd_.sursenofd.G_GetOpenFileName("123");
  return sursenofd.G_GetSaveFileName();
}


_sursen_ofd_reader_ofd_.CopyFile = function (sursenhttp, srcfile, objfile) {

  var sn = sursenhttp.G_CopyFile(srcfile, objfile);
  return sn;
}
//////////////////////////////////////////////////////////////////////////////////////////
_sursen_ofd_reader_ofd_.PrintDoc = function (sursenobj, strURL) {
  var docName = "sursen_" + Math.random();
  var bGray = false;
  sursenobj.sursenReader.printFile(docName, bGray);
}


//7.3.3 打印文件
_sursen_ofd_reader_ofd_.printFile = function (sursenobj, docName, bGray) {
  sursenobj.sursenReader.printFile(docName, bGray);
}

////////////////////////////////////////endend/////////////////////////////////////////////
////////////////////////////////////////append 自动用印和批量用印/////////////////////////
_sursen_ofd_reader_ofd_.loadXml = function (xmlString) {
  var retstring = "";
  domParser = new DOMParser();
  xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
  //var datasource = '<?xml version="1.0" encoding="utf-8" ?> <wsfiles><wsfile mbid='100000160350'><wsmc>提供法律援助通知书</wsmc><firstKey>月</firstKey><secondKey>0;1</secondKey><sealType>1</sealType><sealName>院章</sealName><offx>31</offx><offy>31</offy><spacing>1;0</spacing></wsfile></wsfiles>   	
  var wsfiles = xmlDoc.getElementsByTagName('wsfile');
  var kstr = "sealName,firstKey,secondKey,sealType,offx,offy,step,wsmc";
  var kar = kstr.split(",");

  for (var i = 0; i < wsfiles.length; i++) {
    var vstr = "";
    for (var n = 0; n < kar.length; n++) {

      if (n == 0) vstr = wsfiles[i].children[n].innerHTML;
      else vstr = vstr + "^" + wsfiles[i].children[n].innerHTML;
    }

    var attrs = wsfiles[i].attributes;
    var mbid = attrs[0].value;
    vstr = vstr + "^" + mbid;
    if (i == 0) retstring = vstr;
    else retstring = retstring + "|" + vstr;
    //有值了，下一步不是我的事了
  }
  return retstring.split("|");

}

_sursen_ofd_reader_ofd_.loadXmlEx = function (xmlString) {
  var retstring = "";
  domParser = new DOMParser();
  var xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
  //var datasource = '<?xml version="1.0" encoding="utf-8" ?> <wsfiles><wsfile mbid='100000160350'><wsmc>提供法律援助通知书</wsmc><firstKey>月</firstKey><secondKey>0;1</secondKey><sealType>1</sealType><sealName>院章</sealName><offx>31</offx><offy>31</offy><spacing>1;0</spacing></wsfile></wsfiles>   	
  var wsfiles = xmlDoc.getElementsByTagName('wsfile');

  for (var i = 0; i < wsfiles.length; i++) {
    var vstr = wsfiles[i];
    var attrs = vstr.attributes;
    var mbid = attrs[0].value;
    var tstr = vstr.innerHTML;
    tstr = "<wsfile mbid='" + mbid + "'>" + tstr + "</wsfile>";
    if (i == 0) retstring = tstr;
    else retstring = retstring + "|" + tstr;
    //有值了，下一步不是我的事了
  }
  return retstring.split("|");

}


/**
 * 
 * @param {Object} mbinfos  
 * @param {Object} mbid
 * @param {Object} s_sedoi
 * @param {Object} sfile
 * @param {Object} firstKey
 * @param {Object} secondKey
 * @param {Object} sealname
 * @param {Object} returnInfo
 */
SursenSeal.getSealInfo = function (mbinfos, mbid, s_sedoi, sfile, sealname, returnInfo) {
  var mbinfo = _sursen_ofd_reader_ofd_.findByMbid(mbinfos, mbid);
  var firstKey = mbinfo.firstKey;
  var secondKey = mbinfo.secondKey;
  var sealType = mbinfo.sealType;
  var xoff = mbinfo.offx;
  var yoff = mbinfo.offy;
  var step = mbinfo.spacing;
  if (sealType == "-1") {
    //_sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcfileUrl, "该文书不支持自动用印", null);

    return "";
  }
  var firstinfo = _sursen_ofd_reader_ofd_.S_getPosInfoBykey(s_sedoi, sfile, firstKey);

  //分页处理
  var pages = _sursen_ofd_reader_ofd_.splitPage(firstinfo);
  //排序
  pages = _sursen_ofd_reader_ofd_.PagedesSort(pages);
  //根据关键字选择
  var secondarr;
  if (sealType == "1") {
    secondarr = "";
    var vsecondarr = secondKey.split(";");
    for (var j = 0; j < vsecondarr.length; j++) {
      var tstr = vsecondarr[j] + ",30,277";
      if (secondarr.length < 1) secondarr = tstr;
      else  secondarr = secondarr + ";" + tstr;
    }
  }
  if (sealType == "2")   secondarr = _sursen_ofd_reader_ofd_.S_getPosInfoBykeyEx(s_sedoi, sfile, secondKey, step);
  if (sealType == "3")    secondarr = "";
  //_sursen_ofd_reader_ofd_.debugInfo(true,"第二关键字位置",secondarr);
  //判断是否支持此类文件自动盖章
  var posarr;
  if (secondarr.length < 1) {
    posarr = _sursen_ofd_reader_ofd_.getLocationByDefault(pages);
  }
  else {

    //posarr = _sursen_ofd_reader_ofd_.getLocationByKey(pages, secondarr, step);
    posarr = _sursen_ofd_reader_ofd_.getLocationByPage(pages, secondarr);
  }

  //计算印章位置，//添加印章信息
  //var sealname = jsonobj.wsinfo[i].yzmc;
  var sealinfo = _sursen_ofd_reader_ofd_.getSealPosByKey(posarr, xoff, yoff, sealname);
  return sealinfo;
}


SursenSeal.openOfdFile = function (sfile) {
  var sreader = _g_sursen_ofd_reader_ofd_;
  _sursen_ofd_reader_ofd_.openOfdFile(sreader, sfile, false);

}
//{"wsinfo":{"YZBH":"","YZMC":"","MBBH":"", "MBMC":"","DGZWJ":"","BCDZ","mbfwdz"}],"isdebug":true}
/**
SursenSeal.sealbyAuto = function (wsinfo) {
  var sinfo = "";
  var sfile = "";
  var returnInfo = { success: true, resultList: [] };


  //var sreader = _g_sursen_ofd_reader_ofd_; //加载阅读器对象
  var s_sedfun = _sursen_ofd_reader_ofd_.getSedFun(); //加载下载对象
  var s_sedoi = _sursen_ofd_reader_ofd_.getSedOI();  //加载版式对象

  var proxyUploadUrl = _sursen_ofd_reader_ofd_.ReadConfig_sendfunEx(sreader, "Proxy");
  var changeServerUrl = _sursen_ofd_reader_ofd_.ReadConfig_sendfunEx(sreader, "ChangeServer");
  var tmpPath = _sursen_ofd_reader_ofd_.getTmpPath(s_sedfun);
  //获取文书对象
  var jsonobj = eval("(" + wsinfo + ")");
  //获取所有文书的模板信息
  var mbidstr = _sursen_ofd_reader_ofd_.getMbids(jsonobj.wsinfo);
  var proxyUrl = _sursen_ofd_reader_ofd_.ReadConfig_sendfunEx(sreader, "KeySearch") + mbidstr;
  var mbinfoStr = _sursen_ofd_reader_ofd_.httpUpload_sendfun(s_sedfun, "", proxyUrl);
  var mbinfos = eval("(" + mbinfoStr + ")");
  var sealinfo = "";
  for (var i = 0; i < jsonobj.wsinfo.length; i++) {
    var srcfileUrl = jsonobj.wsinfo[i].dgzwj;
    var exname = srcfileUrl.toLowerCase();
    if (exname.indexOf(".ofd") == -1) {
      sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(s_sedfun, tmpPath, srcfileUrl, "doc");
      var json = _sursen_ofd_reader_ofd_.doc2ofd(s_sedfun, sfile, changeServerUrl, "doc", "ofd");
      if (json.statusCode == "0") {
        _sursen_ofd_reader_ofd_.delFile_sursen(_g_sursen_ofd_reader_ofd_, sfile);
        sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(_g_sursen_ofd_reader_ofd_, tmpPath, json.data, "ofd");
      }
    } else sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(s_sedfun, tmpPath, srcfileUrl, "ofd");
    //获取关键字，	计算印章位置
    var mbid = jsonobj.wsinfo[i].mbbh;
    var yzmc = jsonobj.wsinfo[i].yzmc;
    var yzmcarr = yzmc.split(";");
    var sealname = yzmc;
    sealinfo = "";
    if (yzmcarr.length > 1) {
      var yzlx = jsonobj.wsinfo[i].yzlx;
      var yzlxarr = yzlx.split(";");
      for (var j = 0; j < yzlxarr.length; j++) {
        var mbbh = "";
        var m_sealinfo = "";
        sealname = yzmcarr[j];
        if (yzlxarr[j].indexOf("1") != -1) mbbh = mbid;
        else mbbh = "3_" + mbid;
        m_sealinfo = SursenSeal.getSealInfo(mbinfos, mbbh, s_sedoi, sfile, sealname, returnInfo);
        if (sealinfo.length > 1) sealinfo = sealinfo + ";" + m_sealinfo;
        else sealinfo = m_sealinfo;
      }
    } else sealinfo = SursenSeal.getSealInfo(mbinfos, mbid, s_sedoi, sfile, sealname, returnInfo);

    //加盖印章
    var sok = -1;
    if (sealinfo.length < 2) {
      sealinfo = "该文书不支持自动用印";
    }
    sok = _sursen_ofd_reader_ofd_.sealbyPost(s_sedoi, sfile, sealinfo);
    if (sok != 0) {
      var sealmsg = _sursen_ofd_reader_ofd_.getErrMsg(sok, sealinfo);
      sealmsg = "盖章失败，错误原因为：" + sealmsg + "(错误代码:" + sok + ")";
      _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcfileUrl, sealmsg, sfile);
    }
    else {

      //_sursen_ofd_reader_ofd_.printInfo("盖章文件:", sfile);
      //上传盖章文件
      var saveUrl = jsonobj.wsinfo[i].bcdz;
      //var uploadUrl = encodeURI(encodeURI(proxyUploadUrl + _sursen_ofd_reader_ofd_.replacstrEx(saveUrl, "&", "@@") + "&returnType=1"));
      var vstr = "";
      if (saveUrl.length > 2) vstr = _sursen_ofd_reader_ofd_.httpUpload_reader(s_sedfun, sfile, saveUrl, proxyUploadUrl);
      if (vstr.length > 1) {
        var rinfo = eval("(" + vstr + ")");
        var rvalue = rinfo.returnValue;
        _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, true, srcfileUrl, "成功！", rvalue);
        _sursen_ofd_reader_ofd_.delFile_sursen(s_sedfun, sfile);//删除文件

      } else _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcfileUrl, "盖章成功，保存失败", sfile);
    }


  }

  return returnInfo;
}
**/
/**
 * 设置返回对象信息
 * @param {Object} retInfo
 * @param {Object} issucess
 * @param {Object} url
 * @param {Object} returstr
 */
_sursen_ofd_reader_ofd_.setReturnInfo = function (retInfo, issucess, url, msg, returstr) {
  if (retInfo.success == false) retInfo.success = false;
  else retInfo.success = issucess;
  retInfo.resultList.push({
    success: issucess,
    url: url,
    message: msg,
    returnValue: returstr
  });
}

/**
 * 获取到所有文书的模板串
 * @param {Object} wsinfo {"wsinfo":{"YZBH":"","YZMC":"","MBBH":"", "MBMC":"","DGZWJ":"","BCDZ"}],"isdebug":true}
 */
_sursen_ofd_reader_ofd_.getMbids = function (wsinfos) {
  var mbstr = "";
  var tstr = "";
  for (var i = 0; i < wsinfos.length; i++) {
    var nstr = wsinfos[i].mbbh;
    var yzmc = wsinfos[i].yzmc;
    var yzmcarr = yzmc.split(";");
    if (yzmcarr.length == 2) {
      nstr = nstr + ";" + "3_" + nstr;
    }
    if (i == 0) mbstr = nstr;
    else {
      tstr = ";" + mbstr + ";";
      if (tstr.indexOf(";" + nstr + ";") == -1) mbstr = mbstr + ";" + nstr;
    }
  }
  return mbstr;
}
/**
 * 查找模板对象
 * @param {Object} mbinfos
 * @param {Object} mbid
 */
_sursen_ofd_reader_ofd_.findByMbid = function (mbinfos, mbid) {
  var minfo;
  for (var i = 0; i < mbinfos.length; i++) {
    var nstr = mbinfos[i].mbid;
    if (mbid == nstr) {
      minfo = mbinfos[i];
      break;
    }
  }
  return minfo;
}



//////////////////////////////////////////////////////////以下为关键字计算///////////////
//{wsinfo:[{"yzbh","yzmc","mbbh", "mbmc","dgzwj","bcdz"}],isdebug:true}
_sursen_ofd_reader_ofd_.addSealInfo = function (sealname, postinfo, xoff, yoff) {
  var retstr = "";
  var posarr = postinfo.split(";");
  for (var i = 0; i < posarr.length; i++) {
    var tstr = sealname + "," + posarr[i];
    if (i == 0) retstr = tstr;
    else retstr = retstr + ";" + tstr;
  }
  return retstr;
}

/**
 * 根据关键字晒选位置
 * @param {Object} firstInfo
 * @param {Object} secondinfo
 * @param {Object} step
 */
_sursen_ofd_reader_ofd_.getLocationByKey = function (pages, secondarr, step) {
  var pnum = new Array();
  var spacearr = step.split(";");

  for (var i = 0; i < secondarr.length; i++) {
    var items = secondarr[i].split(",");
    var npage = parseInt(items[0]);
    for (var s = 0; s < pages.length; s++) {
      var details = pages[s][0];
      var detail = details.split(",");
      var cp = parseInt(detail[0]);
      if (npage == cp) {
        pnum[i] = new Array();
        var ncount = new Number(spacearr[i]);
        pnum[i][0] = pages[s][ncount];
        break;
      }
    }


  }
  return pnum;

}

_sursen_ofd_reader_ofd_.getLocationByPage = function (pages, pstr) {
  var stepabs = 100000;
  var sealpos = new Array();
  var sencondPages = _sursen_ofd_reader_ofd_.splitPage(pstr);
  var secondarr = _sursen_ofd_reader_ofd_.PagedesSort(sencondPages);
  //var secondarr=pstr.split(";");
  var isfind = false;
  var ipage = 0;
  for (var i = 0; i < secondarr.length; i++) {
    stepabs = 100000;
    var spos = secondarr[i][0];
    var sposarr = spos.split(","); //page,x1,y1,x2,y2...step
    var item = sposarr[0];
    var spage = parseInt(item); //参考关键字页码
    var sypos = parseInt(sposarr[2]);
    var sxpos = parseInt(sposarr[1]);
    if (isfind) {
      ipage++;
      isfind = false;
    }
    isfind = _sursen_ofd_reader_ofd_.getLocationByPage_detail(pages, spage, stepabs, sealpos, ipage, sxpos, sypos);
    if (isfind == false) {
      spage = spage - 1;
      sypos = 270;
      isfind = _sursen_ofd_reader_ofd_.getLocationByPage_detail(pages, spage, stepabs, sealpos, ipage, sxpos, sypos);

    }


  }
  return sealpos;

}

_sursen_ofd_reader_ofd_.getLocationByPage_detail = function (pages, spage, stepabs, sealpos, ipage, sxpos, sypos) {
  var isfind = false;
  for (var s = 0; s < pages.length; s++) {
    var details = pages[s][0];
    var detail = details.split(",");
    var fpage = parseInt(detail[0]);
    if (spage == fpage) {
      var fpos = pages[s];
      for (var j = 0; j < fpos.length; j++) {
        var fitem = fpos[j].split(",");
        var fypos = parseInt(fitem[2]);
        var fxpos = parseInt(fitem[1]);
        //if (fypos > 260) 	continue; //至于页尾的印章关键字不做处理。
        var iabs = Math.pow(Math.abs(fypos - sypos), 2) + Math.pow(Math.abs(fxpos - sxpos), 2);
        // + Math.abs(fxpos-sxpos);
        if (iabs < stepabs) {
          sealpos[ipage] = new Array();
          sealpos[ipage][0] = pages[s][j];
          stepabs = iabs;
          //ipage++;
          isfind = true;
        }
      }

    }
  }
  return isfind;
}





/**
 * 无关键字定位
 * @param {Object} pages
 * @param {Object} secondarr
 * @param {Object} step
 */
/**
 * 无关键字定位
 * @param {Object} pages
 * @param {Object} secondarr
 * @param {Object} step
 */
_sursen_ofd_reader_ofd_.getLocationByDefault = function (pages) {
  var pnum = new Array();
  pnum[0] = new Array();
  pnum[0][0] = pages[pages.length - 1][0];
  return pnum;

}

/**
 * 
 * @param {Object} firstInfo
 */
_sursen_ofd_reader_ofd_.splitPage = function (firstInfo) {

  var sar = firstInfo.split(";");
  var len = sar.length;
  var npage = -1;
  var start = -1;
  var j = 0;
  var pages = new Array();
  for (var i = 0; i < sar.length; i++) {
    var items = sar[i].split(",");
    var spage = parseInt(items[0]);
    if (npage != spage) {
      j = 0;
      start = start + 1;
      pages[start] = new Array();
      npage = spage;

    }

    pages[start][j] = sar[i];
    j = j + 1;
  }
  return pages;

}
//逆排序
_sursen_ofd_reader_ofd_.PagedesSort = function (pages) {

  for (var i = 0; i < pages.length; i++) {
    pages[i].reverse();
  }
  return pages;

}
/**
 * 计算印章位置信息，增加印章名称
 * @param {Object} kinfo
 * @param {Object} xoff
 * @param {Object} yoff
 */
_sursen_ofd_reader_ofd_.getSealPosByKey = function (sar, xoff, yoff, sealname) {
  var rstr = "";
  for (var i = 0; i < sar.length; i++) {
    var snstr = sar[i][0];
    var kar = snstr.split(",");
    var x1 = new Number(kar[1]);
    var y1 = new Number(kar[2]);
    var x2 = new Number(kar[3]);
    var y2 = new Number(kar[4]);
    var xnew = (x1 + x2) / 2 - xoff;
    var ynew = y1 - yoff;

    var vstr = sealname + "," + kar[0] + "," + xnew + "," + ynew + "," + xnew + "," + ynew + ",0";
    if (i == 0) rstr = vstr;
    else rstr = rstr + ";" + vstr;
  }
  return rstr;
}

//////////////////////////////////////////////////////////以上为关键字计算///////////////


/**
 * 完成批量用印功能 此接口对外提供
 * @param {Object} srcFileUrl 已盖章文件
 * @param {Object} mbFileUrls 需要盖章的文件，可支持多个，中间用分号分隔
 * @param {Object} saveFileUrls 保存路径，需要按照对应的待盖章的顺序提供
 * 返回结果 {'success':'true','failedList':[{‘url':'http://'},{‘url':'http://'}],'message':'失败原因'}
 */
SursenSeal.sealbyBatch = function (srcFileUrl, mbFileUrls, saveFileUrls, isdebug) {
  var returnInfo = { success: true, resultList: [] };
  //var sreader = _g_sursen_ofd_reader_ofd_; //加载阅读器对象
  var s_sedfun =_g_sursen_ofd_reader_ofd_.sursenhttp; //加载下载对象
  var s_sedoi = _g_sursen_ofd_reader_ofd_.sursenofd;  //加载版式对象

  var tmpPath = _g_sursen_ofd_reader_ofd_.g_tmppath;
  var sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(s_sedfun, tmpPath, srcFileUrl, "ofd");
  //var rt = sreader.sursenReader.openFile(sfile,false);
  var sealnameandsealpos = _sursen_ofd_reader_ofd_.getSealPostAndSealName(s_sedoi, sfile);
  _sursen_ofd_reader_ofd_.printInfo("getSealInfo", sealnameandsealpos);

  if (sealnameandsealpos.length > 1) {
    var prxoxy =  _g_sursen_ofd_reader_ofd_.Proxy;
       var dstr = _sursen_ofd_reader_ofd_.sealbyDetail(prxoxy, s_sedoi, s_sedfun, tmpPath, sealnameandsealpos, mbFileUrls, saveFileUrls, returnInfo);
  } else {
    _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcFileUrl, "获取印章失败,请确认源文件地址是否正确或是否盖章!", null);
  }
  return returnInfo;
}



/**
 * 拆分服务
 * @param {Object} srcfile
 * @param {Object} splitRule
 * @param {Object} saveFileurl
 */
_sursen_ofd_reader_ofd_.splitFiles = function (_g_sursen_ofd_reader_ofd_, srcfile, splitRule, saveFileurl) {
  var retstr = "";
  var npage = _g_sursen_ofd_reader_ofd_.sursenofd.GetPageCount(srcfile);
  var savefiles = saveFileurl.split(";");
  var splits = splitRule.split(";");  
  if (splits.length < 2) {
    var guid = getGUID() + ".ofd";
    var tmppath = _sursen_ofd_reader_ofd_.getTmpPath(_g_sursen_ofd_reader_ofd_);
    var mbfile = tmppath;
    for (var i = 0; i < splits; i++) {
      var j = i + 1;
      var sn = _sursen_ofd_reader_ofd_.ExtractOFDPages(srcfile, j, mbfile);
      var kn = _sursen_ofd_reader_ofd_.httpUpload_sendfun(mbfile, savefiles);
      if (i == 0) retstr = "" + kn;
      else retstr = retstr + ";" + kn;
    }
  }
  return retstr;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////



_sursen_ofd_reader_ofd_.httpUpload_readerEX = function (sursenobj, sfile, uploadUrl, proxyUrl) {
  var sok = false;
  var jsinfostr = "";
  var serverStr = "{'code':'0','success':true,'message':'操作成功','data':{'dir':'#','name':'$','storageResult':{'md5':'5b8643b69484fe9aaf4da6211becc563','success':true},'storageDeleteResult':null}}";
  //var proxyUrl=_sursen_ofd_reader_ofd_.ReadConfig_sendfun(sursenobj,"config_project_firefox.ini","","Proxy");
  var rt = sursenobj.sursenReader.openFile(sfile, false);
  if (rt) {
    var objstr = _sursen_ofd_reader_ofd_.replacstrEx(uploadUrl, "&", "$");
    var dirstr = _sursen_ofd_reader_ofd_.getRangStr(objstr, "dir=", "$");
    var namestr = _sursen_ofd_reader_ofd_.getRangStr(objstr, "name=", "$");
    serverStr = serverStr.replace("#", dirstr);
    serverStr = serverStr.replace("$", namestr);

    var saveurl = proxyUrl + encodeURI(encodeURI(_sursen_ofd_reader_ofd_.replacstrEx(uploadUrl, "&", "@@")));
    sok = sursenobj.sursenReader.saveFile(saveurl + "&returnType=0");
    sursenobj.sursenReader.closeFile();
    if (sok) {
      jsinfostr = "{'success':true,'returnValue':" + serverStr + "}";
    } else {
      jsinfostr = "{'success':false,'returnValue':null}";
    }
  } else jsinfostr = "{'success':false,'returnValue':null}";
  return jsinfostr;
}




_sursen_ofd_reader_ofd_.getRangStr = function (surl, startkey, endkey) {
  var sindex = surl.indexOf(startkey);
  sindex = sindex + startkey.length;
  var tstr = surl.substring(sindex, surl.length);
  var offx = tstr.indexOf(endkey);
  if (offx == -1) offx = tstr.length;
  var nindex = sindex + offx;
  var instr = surl.substring(sindex, nindex);
  return instr;
}




_sursen_ofd_reader_ofd_.sealbyDetail = function (proxyUrl, sedoi, sedfun, tmpPath, sealnameandsealpos, mbFileUrls, saveFileUrls, returnInfo) {
  //var returnInfo={success: true,resultList:[]};
  var sealfiles = mbFileUrls.split(";");
  var savefiles = saveFileUrls.split(";");
  //var proxyUrl=_sursen_ofd_reader_ofd_.ReadConfig(sread,"Proxy");

  for (var i = 0; i < sealfiles.length; i++) {
    var srcFileUrl = sealfiles[i];
        srcFileUrl=_sursen_ofd_reader_ofd_.getFilebyHttps(_sursen_ofd_reader_ofd_, srcFileUrl);
    var sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(sedfun, tmpPath, srcFileUrl, "ofd");
    var sealok = _sursen_ofd_reader_ofd_.sealbyPost(sedoi, sfile, sealnameandsealpos);
    var json;
    if (sealok == 0) { //盖章成功			
      var upstr = "";
      if (savefiles[i].length > 1) upstr = _sursen_ofd_reader_ofd_.httpUpload_reader(sedfun, sfile, savefiles[i], proxyUrl);
      if (upstr.length > 1) { //上传成功
        json = _sursen_ofd_reader_ofd_.parseJson(upstr);
        _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, true, srcFileUrl, "成功！", json.returnValue);
        _sursen_ofd_reader_ofd_.delFile_sursen(sedfun, sfile);
      } else {
        _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcFileUrl, "盖章成功，保存失败！", sfile);
      }
    } else {
      var msg = _sursen_ofd_reader_ofd_.getErrMsg(sealok, sealnameandsealpos);
      _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcFileUrl, "盖章失败,错误原因为：" + msg, null);
    }

  }
  return returnInfo;

}

_sursen_ofd_reader_ofd_.getErrMsg = function (errcode, sealnameandsealpos) {
  var retmsg = "";
  var eid = new Number(errcode);
  if (errcode == -20) retmsg = "盖章页面不存在(" + sealnameandsealpos + ")";
  else retmsg = "公章不存在(" + sealnameandsealpos + ")";
  return retmsg;
}
_sursen_ofd_reader_ofd_.replacstrEx = function (srcstr, oldstr, newstr) {
  var nstr = srcstr;
  while (nstr.indexOf(oldstr) != -1) {
    nstr = nstr.replace(oldstr, newstr);
  }
  return nstr;
}
/**
 * 获取印章位置信息
 * @param {Object} srcFile
 */
_sursen_ofd_reader_ofd_.getSealPostAndSealName = function (sursenofd, srcfile) {
  var hDoc = sursenofd.S_OpenFile(srcfile, "");
  var sealInfo = sursenofd.S_GetSealPosName();
  var hClose = sursenofd.S_CloseFileByHandle(hDoc);
  return sealInfo;
}
/**
 * 完成静默盖章功能
 * @param {Object} srcfile
 * @param {Object} sealnameandsealpost 印章名称和位置信息
 */
_sursen_ofd_reader_ofd_.sealbyPost = function (sursenofd, srcfile, sealinfo) {
  var sar = sealinfo.split(";");
  var hDoc = sursenofd.S_OpenFile(srcfile, "");
  var seal = sursenofd.S_Seal(sealinfo, "");
  var hClose = sursenofd.S_CloseFileByHandle(hDoc);
  return seal;
}


_sursen_ofd_reader_ofd_.S_getPosInfoBykey = function (sursenofd, srcfile, keystr) {
  var hDoc = sursenofd.S_OpenFile(srcfile, "");
  var textinfo = sursenofd.S_GetTextPos(keystr);
  var hClose = sursenofd.S_CloseFileByHandle(hDoc);
  return textinfo;
}
//传入多个关键字,中间用分号分割
_sursen_ofd_reader_ofd_.S_getPosInfoBykeyEx = function (sursenofd, srcfile, keystr, step) {
  var rar = "";

  var sar = keystr.split(";");
  //var steps=step.split(";");
  var hDoc = sursenofd.S_OpenFile(srcfile, "");

  var j = 0;
  for (var i = 0; i < sar.length; i++) {
    var vstr = sursenofd.S_GetTextPos(sar[i].trim());

    if (vstr.length > 1) {
      if (rar.length < 1) rar = vstr;
      else rar = rar + ";" + vstr;
    }
  }
  var hClose = sursenofd.S_CloseFileByHandle(hDoc);
  return rar;
}

//根据关键字完成盖章功能
_sursen_ofd_reader_ofd_.S_SealBykey = function (_g_sursen_ofd_reader_ofd_, srcfile, firstKey, secondKey, pwszSealName, step, offx, offy) {
  var hDoc = _g_sursen_ofd_reader_ofd_.sursenofd.S_OpenFile(srcfile, "");
  var textinfo = _g_sursen_ofd_reader_ofd_.sursenofd.S_GetTextPos(keystr);
  if (secondKey.length > 1) {
    secondinfo = _g_sursen_ofd_reader_ofd_.sursenofd.S_GetTextPos(keystr);
  }
  //根据主次关键字完成位置定位

  var para = txtinfo.split(",");
  var npage = new Number(para[0]);
  var lx = new Number(para[1]);
  var ly = new Number(para[2]);
  var rx = new Number(para[1]);
  var ry = new Number(para[2]);
  var px = (lx + rx) / 2 ;
  var py = (ly + ry) / 2 ;
  var ox = new Nubmer(offx);
  var oy = new Nubmer(offy);
  px = px - ox;
  py = py - oy;
  var sealok = _g_sursen_ofd_reader_ofd_.sursenofd.S_AddSealEx(pwszSealName, "", npage, px, py, false);
  _g_sursen_ofd_reader_ofd_.sursenofd.S_SaveFile();
  var hClose = _g_sursen_ofd_reader_ofd_.sursenofd.S_CloseFileByHandle(hDoc);
  return sealok;

}




//long S_AddSealEx(string pwszSealName, string pwszPinOrCerAliaName, long nPage, long x, long y, boolean bBlock)
//基于位置盖章

_sursen_ofd_reader_ofd_.AddSealEx = function (_g_sursen_ofd_reader_ofd_, pwszSealName, nPage, x, y) {
  var hDoc = _g_sursen_ofd_reader_ofd_.sursenofd.S_OpenFile(srcfile, "");
  var sealok = _g_sursen_ofd_reader_ofd_.sursenofd.S_AddSealEx(pwszSealName, "", nPage, x, y, false);
  var hClose = _g_sursen_ofd_reader_ofd_.sursenofd.S_CloseFileByHandle(hDoc);
  return sealok;

}



_sursen_ofd_reader_ofd_.downloadFileEx_sendfun = function (_g_sursen_ofd_reader_ofd_, path, hurl, mbfile) {

  var sn = _g_sursen_ofd_reader_ofd_.sursenhttp.HttpDownLoadFile(path, hurl, mbfile);

  var sfile = path + mbfile;
  if (_g_sursen_ofd_reader_ofd_.g_delfiels.length < 1) _g_sursen_ofd_reader_ofd_.g_delfiels = sfile;
  else _g_sursen_ofd_reader_ofd_.g_delfiels = _g_sursen_ofd_reader_ofd_.g_delfiels + ";" + sfile;

  return sfile;
}

_sursen_ofd_reader_ofd_.downloadFile_sendfun = function (sursenhttp, path, hurl, extname) {
  var sfile="";
  var surl=hurl.toLowerCase();
  var sn=false;
  var mbfile = path + _sursen_ofd_reader_ofd_.getGUID() + "." + extname;
  if(surl.indexOf("https")!=-1){	    
	  	var cookstr= document.cookie;
	  	sn =sursenhttp.G_downFileCookie(hurl,mbfile,cookstr);

  }else{
	  //sfile=_sursen_ofd_reader_ofd_.downloadFile_http(sursenhttp, path, hurl, extname); 
	   var tsfile = _sursen_ofd_reader_ofd_.getGUID() + "." + extname;
       sn = sursenhttp.HttpDownLoadFile(path, hurl, tsfile);
       mbfile = path + tsfile;  
       
  }
  return mbfile;
}




_sursen_ofd_reader_ofd_.ReadConfig_sendfunEx = function (readprent, key) {
  var configInfo = "";
  if (key.indexOf("Proxy") != -1) configInfo = _g_sursen_ofd_reader_ofd_.Proxy;
  if (key.indexOf("KeySearch") != -1) configInfo = _g_sursen_ofd_reader_ofd_.KeySearch;
  if (key.indexOf("ChangeServer") != -1) configInfo = _g_sursen_ofd_reader_ofd_.ChangeServer;
  return configInfo;
}

_sursen_ofd_reader_ofd_.ReadConfigBysendfun = function (sedfun, key) {
  var configInfo = sedfun.ReadConfig("/opt/sursen/reader/config_project_firefox.ini", "General", key);

  return configInfo;
}


_sursen_ofd_reader_ofd_.ReadConfig = function (readerobject, key) {
  var configInfo = _sursen_ofd_reader_ofd_.ReadConfig_sendfunEx(null, key);
  return configInfo;
}


/**
 * 删除文件
 * @param {Object} srcfiles
 */
_sursen_ofd_reader_ofd_.delFile_sursen = function (sursenhttp, srcfiles) {
  var sar = srcfiles.split(";");
  for (var i = 0; i < sar.length; i++) {
    var sfile = sar[i];
    //sfile = sfile.replace(/\\/ig,"/");
    sursenhttp.G_DeleteFile(sfile);
  }
  //_g_sursen_ofd_reader_ofd_.g_delfiels="";
}




//long S_ExtractOFDPages (string srcA, string pageListA, string dstC)
//S_GetPageCount
/**
 * 获取文件页数
 * @param {Object} srcfile
 */
_sursen_ofd_reader_ofd_.GetPageCount = function (_g_sursen_ofd_reader_ofd_, srcfile) {
  var hDoc = _g_sursen_ofd_reader_ofd_.sursenofd.S_OpenFile(srcfile, "");
  var pagenum = _g_sursen_ofd_reader_ofd_.sursenofd.S_GetPageCount();
  var hClose = _g_sursen_ofd_reader_ofd_.sursenofd.S_CloseFileByHandle(hDoc);
  return pagenum;
}

_sursen_ofd_reader_ofd_.ExtractOFDPages = function (_g_sursen_ofd_reader_ofd_, srcA, pageListA, dstC) {
  var sn = _g_sursen_ofd_reader_ofd_.sursenofd.S_ExtractOFDPages(srcA, pageListA, dstC);
  return sn;
}


/**
 * 获取印章图片
 * @param {Object} sealname
 * @param {Object} pin
 */
_sursen_ofd_reader_ofd_.GetSealImage = function (_g_sursen_ofd_reader_ofd_, sealname, pin) {
  var sn = _g_sursen_ofd_reader_ofd_.sursenofd.S_GetSealImage(sealname, pin);
  return sn;
}
///////////////////////////////////////////////////////////////
////////////////////////////////////sursenReader////////////////
//////////////////////////////////////////////////////////

_sursen_ofd_reader_ofd_.ReadConfig_sendfun = function (_g_sursen_ofd_reader_ofd_, configfile, section, key) {
  var configInfo = _g_sursen_ofd_reader_ofd_.sursenReader.ReadConfig(configfile, "", key);

  return configInfo;
}


_sursen_ofd_reader_ofd_.printInfo = function (method, msg) {
  console.log(method + ":" + msg)
}
/**
 * 解析为jsoon对象
 * @param {Object} jsonstr
 */
_sursen_ofd_reader_ofd_.parseJson = function (jsonstr) {
  var json = eval('(' + jsonstr + ')');
  return json;
}


_sursen_ofd_reader_ofd_.getExtName = function (srcfile) {
  var lastlen = srcfile.lastIndexOf(".");
  var nlen = srcfile.length;
  var ename = srcfile.substring(lastlen + 1, nlen);
  return ename.toLowerCase();
}
//var url ="http://127.0.0.1:8080/gzwzb/fileConvertService/getOfdFile.do?srcType="+stype+"&destType="+mtype+"&math="+Math.random();

_sursen_ofd_reader_ofd_.doc2ofd = function (sedfun, srcfile, serverUrl, srcType, destType) {
  var surl = serverUrl + "?srcType=" + srcType + "&destType="  + "&sysid=10&unitcode=10&random=" + Math.random();
  var retinfo = _sursen_ofd_reader_ofd_.httpUpload_sendfun(sedfun, srcfile, surl + "&returnType=1");
  var json = _sursen_ofd_reader_ofd_.parseJson(retinfo);
  return json;
}

_sursen_ofd_reader_ofd_.getWin = function () {
  var sUserAgent = navigator.userAgent;
  var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
  return isWin;

}

_sursen_ofd_reader_ofd_.debugInfo = function (isdebug, msg, svalue) {
  //if(isdebug) alert("期望结果：" + msg + "实际结果：" + svalue);
}

//生成关键串
_sursen_ofd_reader_ofd_.getReturnWsFile = function (srcfle, sealName, firstKey, secondKey, sealType, offx, offy, spacing) {
  var returStr = "";
  if (srcfle.indexOf("[") == -1) {
    alert("文件格式不对，请重新选择");

  } else {
    var sfiles = srcfle.split("[");
    var marstr = sfiles[1];
    var mar = marstr.split("]");
    var mbid = "#mbid:" + mar[0];
    var wsmc = "#wsmc:" + mar[1].split(".")[0];
    returStr = mbid + "|" + wsmc + "|" + "#firstkey:" + firstKey + "|" + "#secondKey:" + secondKey + "|" + "#sealType:" + sealType + "|#sealName:" + sealName + "|#offx:" + offx + "|#offy:" + offy + "|#spacing:" + spacing;
  }
  return returStr;

}
//返回替换的关键串
_sursen_ofd_reader_ofd_.replaceWsFile = function (keyparestr) {
  var g_wsfile = "<wsfile mbid='#mbid'><wsmc>#wsmc</wsmc><firstKey>#firstkey</firstKey><secondKey>#secondKey</secondKey><sealType>#sealType</sealType><sealName>#sealName</sealName><offx>#offx</offx><offy>#offy</offy><spacing>#spacing</spacing></wsfile>";
  var returStr = g_wsfile;
  var kar = keyparestr.split("|");
  for (var i = 0; i < kar.length; i++) {
    var dar = kar[i].split(":");
    returStr = returStr.replace(dar[0], dar[1]);
  }

  return returStr;

}


// 输入 {"srcFiles":[,,,],"printNums":[2,1,3]}
// 返回 {"srcFiles":[,,,],"printNums":[2,1,3]，"sucecss",[true,false,true],"info",['','','']}
_sursen_ofd_reader_ofd_.batchPrint = function (srcFiles, printNums) {
  var printInfo = _sursen_ofd_reader_ofd_.getPrintInfo();
  var dobj = _sursen_ofd_reader_ofd_.getSedOI();
  var sedfunobj=_sursen_ofd_reader_ofd_.getSedFun();
  var tmppath=_sursen_ofd_reader_ofd_.getTmpPath(sedfunobj);
  for (var i = 0; i < srcFiles.length; i++) {
	var ishttp=_sursen_ofd_reader_ofd_.getIsHttps(srcFiles[i]);
    var sfile=srcFiles[i];
	if(ishttp) sfile=_sursen_ofd_reader_ofd_.downloadFile_https(sedfunobj, tmppath, srcFiles[i], "ofd");
	printInfo = _sursen_ofd_reader_ofd_.PrintDocEx(dobj,sfile, printNums[i], printInfo);
	if(ishttp) _sursen_ofd_reader_ofd_.delFile_sursen (sedfunobj, sfile);
  }
  return printInfo;
}



_sursen_ofd_reader_ofd_.getPrintInfo = function () {
  var printInfo = {};
  printInfo.info = new Array();
  printInfo.success = new Array();
  printInfo.srcfile = new Array();
  printInfo.printnum = new Array();
  return printInfo;

}

_sursen_ofd_reader_ofd_.generaSureSenOfd = function () {
  var div = document.createElement("div");
  var gid = "P" + _sursen_ofd_reader_ofd_.getGUID();
  div.innerHTML = "<embed id='" + gid + "' " + "type=\"application/SursenOFD-plugin\" style=\"display: block; width: 0px; height: 0px;\"></embed>";
  document.body.appendChild(div);
  var dobj = document.getElementById(gid);
  return dobj;
}


_sursen_ofd_reader_ofd_.PrintDocEx = function (ofdobjec, srcFilrUrl, printnum, printInfo) {
  var sfile = "";
  var info = "";
  var success = false;
  var nprint = parseInt(printnum);

  var handle = ofdobjec.S_OpenFile(srcFilrUrl, "");
  if (handle == '00000000') {
    info = "文件打印失败(" + srcFilrUrl + "不存在)";
  } else {
    var sn = ofdobjec.S_PrintDoc("", nprint, true, "");
    if (sn != 0) {
      //info = dobj.S_GetLastError();
      if (sn == 255) info = "打印失败(不支持的打印机)!";
      else info = "文件打印失败!";
    } else {
      info = "打印成功";
      success = true;
    }
    //var hClose = readerParent.sursenofd.S_CloseFileByHandle(hDoc);
    var hClose = ofdobjec.S_CloseFileByHandle(handle);
  }
  printInfo.info.push(info);
  printInfo.success.push(success);
  printInfo.srcfile.push(srcFilrUrl);
  printInfo.printnum.push(nprint);
  return printInfo;
}

/**
 * 15 用印文件打印接口
 * @param {Object} sursenobj
 * @param {Object} count
 */

_sursen_ofd_reader_ofd_.printOfdFile = function (sursenobj, count) {
  var smsg = "";
  var printInfo = _sursen_ofd_reader_ofd_.getPrintInfo();
  var srcFilrUrl = sursenobj.currentofdfile;
  var ishttp=_sursen_ofd_reader_ofd_.getIsHttps(srcFilrUrl);
  
  if(ishttp){

	  srcFilrUrl=_sursen_ofd_reader_ofd_.downloadFile_https(_g_sursen_ofd_reader_ofd_.sursenhttp, _g_sursen_ofd_reader_ofd_.g_tmppath, srcFilrUrl, "ofd");
  }
  var s_sedoi = _sursen_ofd_reader_ofd_.getSedOI();  //加载版式对象
  var sn = _sursen_ofd_reader_ofd_.PrintDocEx(s_sedoi, srcFilrUrl, count, printInfo);
  if (!printInfo.success[0]) {
    smsg = printInfo.info[0];
    alert(printInfo.info[0]);
  }
  if(ishttp){ _sursen_ofd_reader_ofd_.delFile_sursen (_g_sursen_ofd_reader_ofd_.sursenhttp, srcFilrUrl);
  }
  if (smsg.length > 1) throw Error(smsg);

}

_sursen_ofd_reader_ofd_.getReader = function () {
  var ocxstr = "trivial/very";
  return _sursen_ofd_reader_ofd_.getOcxObject(ocxstr);

}

_sursen_ofd_reader_ofd_.getSedFun = function () {
  var ocxstr = "application/httpconnect-plugin";
  return _sursen_ofd_reader_ofd_.getOcxObject(ocxstr);

}

_sursen_ofd_reader_ofd_.getSedOI = function () {
  var ocxstr = "application/SursenOFD-plugin";
  return _sursen_ofd_reader_ofd_.getOcxObject(ocxstr);

}


_sursen_ofd_reader_ofd_.getOcxObject = function (ocxstr) {
  var div = document.createElement("div");
  var gid = "P" + _sursen_ofd_reader_ofd_.getGUID();
  div.innerHTML = "<embed id='" + gid + "' " + "type='" + ocxstr + "' style=\"display: block; width: 0px; height: 0px;\"></embed>";
  document.body.appendChild(div);
  var dobj = document.getElementById(gid);
  return dobj;
}

//新增安全网关处理方法...............................


_sursen_ofd_reader_ofd_.saveOfdFileByHttps = function (sursenobj, url) {
         var sok=false;
		 var saveUrl=decodeURIComponent(url);         
		 var inifile=sursenobj.g_tmppath + _sursen_ofd_reader_ofd_.getGUID() + ".ini";
		 
		 var sinfo = sursenobj.sursenReader.saveFileGm(saveUrl,inifile);
		 if(sinfo.indexOf("true")!=-1){ sok=true;}
		 else {sok=false;}
		 return sok;
}

SursenSeal.sealbyAuto = function (wsinfo) {
  var sinfo = "";
  var sfile = "";
  var returnInfo = { success: true, resultList: [] };


  //var sreader = _g_sursen_ofd_reader_ofd_; //加载阅读器对象
  var s_sedfun = _g_sursen_ofd_reader_ofd_.sursenhttp; //加载下载对象
  var s_sedoi = _g_sursen_ofd_reader_ofd_.sursenofd;  //加载版式对象

  var proxyUploadUrl = _g_sursen_ofd_reader_ofd_.Proxy;
  var changeServerUrl = _g_sursen_ofd_reader_ofd_.ChangeServer;
  var tmpPath = _g_sursen_ofd_reader_ofd_.g_tmppath;
  //获取文书对象
  var jsonobj = eval("(" + wsinfo + ")");
  //获取所有文书的模板信息
  var mbidstr = _sursen_ofd_reader_ofd_.getMbids(jsonobj.wsinfo);
  var proxyUrl = _g_sursen_ofd_reader_ofd_.KeySearch + mbidstr;
  var mbinfoStr = _sursen_ofd_reader_ofd_.httpUpload_sendfun(s_sedfun, "", proxyUrl);
  var mbinfos = eval("(" + mbinfoStr + ")");
  var sealinfo = "";
  for (var i = 0; i < jsonobj.wsinfo.length; i++) {
    var srcfileUrl = jsonobj.wsinfo[i].dgzwj;
	srcfileUrl=_sursen_ofd_reader_ofd_.getFilebyHttps(_g_sursen_ofd_reader_ofd_, url);
    var exname = srcfileUrl.toLowerCase();
    if (exname.indexOf(".ofd") == -1) {

      sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(s_sedfun, tmpPath, srcfileUrl, "doc");
      var json = _sursen_ofd_reader_ofd_.doc2ofd(s_sedfun, sfile, changeServerUrl, "doc", "ofd");
      if (json.statusCode == "0") {
        _sursen_ofd_reader_ofd_.delFile_sursen(_g_sursen_ofd_reader_ofd_, sfile);
        sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(_g_sursen_ofd_reader_ofd_, tmpPath, json.data, "ofd");
      }
    } else sfile = _sursen_ofd_reader_ofd_.downloadFile_sendfun(s_sedfun, tmpPath, srcfileUrl, "ofd");
    //获取关键字，	计算印章位置
    var mbid = jsonobj.wsinfo[i].mbbh;
    var yzmc = jsonobj.wsinfo[i].yzmc;
    var yzmcarr = yzmc.split(";");
    var sealname = yzmc;
    sealinfo = "";
    if (yzmcarr.length > 1) {
      var yzlx = jsonobj.wsinfo[i].yzlx;
      var yzlxarr = yzlx.split(";");
      for (var j = 0; j < yzlxarr.length; j++) {
        var mbbh = "";
        var m_sealinfo = "";
        sealname = yzmcarr[j];
        if (yzlxarr[j].indexOf("1") != -1) mbbh = mbid;
        else mbbh = "3_" + mbid;
        m_sealinfo = SursenSeal.getSealInfo(mbinfos, mbbh, s_sedoi, sfile, sealname, returnInfo);
        if (sealinfo.length > 1) sealinfo = sealinfo + ";" + m_sealinfo;
        else sealinfo = m_sealinfo;
      }
    } else sealinfo = SursenSeal.getSealInfo(mbinfos, mbid, s_sedoi, sfile, sealname, returnInfo);

    //加盖印章
    var sok = -1;
    if (sealinfo.length < 2) {
      sealinfo = "该文书不支持自动用印";
    }
    sok = _sursen_ofd_reader_ofd_.sealbyPost(s_sedoi, sfile, sealinfo);
    if (sok != 0) {
      var sealmsg = _sursen_ofd_reader_ofd_.getErrMsg(sok, sealinfo);
      sealmsg = "盖章失败，错误原因为：" + sealmsg + "(错误代码:" + sok + ")";
      _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcfileUrl, sealmsg, sfile);
    }
    else {

      //_sursen_ofd_reader_ofd_.printInfo("盖章文件:", sfile);
      //上传盖章文件
      var saveUrl = jsonobj.wsinfo[i].bcdz;
      //var uploadUrl = encodeURI(encodeURI(proxyUploadUrl + _sursen_ofd_reader_ofd_.replacstrEx(saveUrl, "&", "@@") + "&returnType=1"));
      var vstr = "";
      if (saveUrl.length > 2) vstr = _sursen_ofd_reader_ofd_.httpUpload_reader(s_sedfun, sfile, saveUrl, proxyUploadUrl);
      if (vstr.length > 1) {
        var rinfo = eval("(" + vstr + ")");
        var rvalue = rinfo.returnValue;
        _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, true, srcfileUrl, "成功！", rvalue);
        _sursen_ofd_reader_ofd_.delFile_sursen(s_sedfun, sfile);//删除文件

      } else _sursen_ofd_reader_ofd_.setReturnInfo(returnInfo, false, srcfileUrl, "盖章成功，保存失败", sfile);
    }


  }

  return returnInfo;
}

_sursen_ofd_reader_ofd_.downloadFile_http = function (sursenhttp, path, hurl, extname) {
	   var mbfile = _sursen_ofd_reader_ofd_.getGUID() + "." + extname;
       var sn = sursenhttp.HttpDownLoadFile(path, hurl, mbfile);
       var sfile = path + mbfile;  
        return sfile;
	}

_sursen_ofd_reader_ofd_.downloadFile_https = function (sursenhttp, path, hurl, extname) {	 
	  	var mbfile = path + _sursen_ofd_reader_ofd_.getGUID() + "." + extname;
	  	var cookstr= document.cookie;
	  	var sn =sursenhttp.G_downFileCookie(hurl,mbfile,cookstr);	 
	     return mbfile;
	}


_sursen_ofd_reader_ofd_.getIsHttps = function(hurl) {
	   var breturn=false;
	   var surl=hurl.toLowerCase();
	   if(surl.indexOf("https")!=-1) breturn=true;
	   return breturn;
}
_sursen_ofd_reader_ofd_.httpUpload_reader = function (sursenhttp, sfile, uploadUrl, proxyUrl) {

  var jsinfostr = "";
  var saveUrl="";
  var surl=uploadUrl.toLowerCase();
  if(surl.indexOf("https")!=-1){ 
	  saveUrl = decodeURIComponent(uploadUrl);	  
	  var replyIniFile=sfile + ".ini";
	  jsinfostr = sursenhttp.G_UpdateFileGM(saveUrl,sfile,replyIniFile);
	  if(jsinfostr.length>1){
		  var reg = /"/g;
		  jsinfostr = jsinfostr.replace(reg, "\\\"");
		  jsinfostr= "{\"message\":\"\",\"returnValue\":\"" + jsinfostr + "\",\"success\":true}";
		  
		  
	  }
   
	  _sursen_ofd_reader_ofd_.delFile_sursen(sursenhttp,replyIniFile);
  }else{
	  saveUrl = proxyUrl + encodeURI(encodeURI(_sursen_ofd_reader_ofd_.replacstrEx(uploadUrl, "&", "@@")));
      jsinfostr = sursenhttp.UpLoadFileEx2(sfile, saveUrl + "&returnType=1");
  }
  return jsinfostr;
}

_sursen_ofd_reader_ofd_.httpsReplace = function(vstr){
   var reg = /"/g;
   var strURL = vstr.replace(reg, "'");
   var strsn=strURL.substring(1,strURL.length-1);
   strsn='"' + strsn + '"';
   return strsn;
}

_sursen_ofd_reader_ofd_.httpUpload_reader_http = function (sursenhttp, sfile, uploadUrl, proxyUrl) {
  var saveUrl = proxyUrl + encodeURI(encodeURI(_sursen_ofd_reader_ofd_.replacstrEx(uploadUrl, "&", "@@")));
  var jsinfostr = sursenhttp.UpLoadFileEx2(sfile, saveUrl + "&returnType=1");
  return jsinfostr;
}

_sursen_ofd_reader_ofd_.httpUpload_reader_https = function (sursenhttp, sfile, uploadUrl, proxyUrl) {

	  
	  var saveUrl = decodeURIComponent(uploadUrl);	  
	  var replyIniFile=sfile + ".ini";
	  var jsinfostr = sursenhttp.G_UpdateFileGM(saveUrl,sfile,replyIniFile);
		
	  return jsinfostr;
	}

window.sursenOfdReader = _sursen_ofd_reader_ofd_;

window.sursenBatchSeal = SursenSeal;

